package com.cn.unarmed.views;

import android.app.Dialog;
import android.content.Context;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cn.unarmed.commonutils.AppUtils;
import com.cn.unarmed.activity.R;

import java.util.HashMap;
import java.util.List;

/**
 * 第一个自定义的组件，弹框
 */
public class DialogOfMyFirst {

    private Display display;
    private Dialog dialog;
    private Context context;

    LinearLayout lLayout_bg;
    ImageView dialog_cancel,iv_cancle;
    TextView dialog_title;
    ListView listview;
    Button button;

    List<HashMap<String, Object>> name;
    HashMap<String, Object> selectMap;

    AlertAdapter adapter;

    private OnItemClickListener listener;
    private OnButtonClickListener listenerButton;

    public DialogOfMyFirst(Context context) {
        this.context = context;
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        display = windowManager.getDefaultDisplay();
    }

    public DialogOfMyFirst builder(List<HashMap<String, Object>> list) {
        name = list;
        View view = LayoutInflater.from(context).inflate(R.layout.diaolog_my, null);

        lLayout_bg = (LinearLayout) view.findViewById(R.id.lLayout_bg);
//        dialog_clear = (ImageView) view.findViewById(R.id.dialog_clear);
        dialog_cancel = (ImageView) view.findViewById(R.id.dialog_cancel);
        dialog_title = (TextView) view.findViewById(R.id.dialog_title);
        listview = (ListView) view.findViewById(R.id.dialog_list);
        button = (Button) view.findViewById(R.id.dialog_ok);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });

        iv_cancle = (ImageView)view.findViewById(R.id.iv_cancle);
        iv_cancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });

        adapter = new AlertAdapter();
        listview.setAdapter(adapter);
//        AppUtils.setListViewHeightBasedOnChildren(listview);

//      定义布局和参数
        dialog = new Dialog(context, R.style.AlertDialogStyle);
        dialog.setContentView(view);

        Window window = dialog.getWindow();
        window.setGravity(Gravity.BOTTOM);

        WindowManager.LayoutParams p = dialog.getWindow().getAttributes();
        p.alpha = 0.9f;
        p.height = (int)display.getHeight();
        p.width = (int)display.getWidth();
        dialog.getWindow().setAttributes(p);

//        调整dialog的窗体大小
        lLayout_bg.setLayoutParams(new FrameLayout.LayoutParams((int) display.getWidth(),
                ViewGroup.LayoutParams.MATCH_PARENT));
        return this;
    }

    public DialogOfMyFirst setListClick(OnItemClickListener listener) {
        this.listener = listener;
        return this;
    }

    public DialogOfMyFirst setTitle(String title) {
        if (title != null) {
            dialog_title.setText(title);
        }
        return this;
    }

    public DialogOfMyFirst setCancel(final View.OnClickListener listener) {
        dialog_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onClick(v);
                dialog.dismiss();
            }
        });
        return this;
    }

    public DialogOfMyFirst setJump(OnButtonClickListener listenerButton) {
        this.listenerButton = listenerButton;
        return this;
    }

    public void show() {
        if (adapter == null) {
            adapter = new AlertAdapter();
            AppUtils.setListViewHeightBasedOnChildren(listview);
        }
        dialog.show();
    }

    public interface OnItemClickListener {
        public void getItem(HashMap<String, Object> map);
    }

    public interface OnButtonClickListener {
        public void getButton(HashMap<String, Object> map);
    }

    private class AlertAdapter extends BaseAdapter {

        public AlertAdapter() {
            selectMap = new HashMap<>();
        }

        @Override
        public int getCount() {
            return name.size();
        }

        @Override
        public Object getItem(int position) {
            return name.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            Holder holder;
            if (convertView == null) {
                LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = layoutInflater.inflate(R.layout.dialog_list_item, null);
                holder = new Holder();
                holder.text_name = (TextView) convertView.findViewById(R.id.item_text);
                holder.item_lay = (RelativeLayout) convertView.findViewById(R.id.item_lay);
                convertView.setTag(holder);
            } else {
                holder = (Holder) convertView.getTag();
            }
            holder.text_name.setText(name.get(position).get("text").toString());
            holder.item_lay.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.getItem(name.get(position));
                    dialog.dismiss();
                }
            });
            return convertView;
        }

        class Holder {
            RelativeLayout item_lay;
            TextView text_name;
        }
    }
}
